package com.ybkj.daijia.server.event.listener;


import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiEmployV4;
import com.ybkj.daijia.api.jpush.JiGuangEmployV4;
import com.ybkj.daijia.server.event.model.DriverAutoOfflineEvent;
import com.ybkj.daijia.server.event.model.DriverTimeEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverTimeService;
import com.ybkj.daijia.server.service.OrderService;
import com.ybkj.daijia.server.websocket.WebSocketSend;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class DriverAutoOfflineEventListener implements ApplicationListener<DriverAutoOfflineEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(DriverAutoOfflineEventListener.class);

    @Autowired
    private OrderService orderService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private DriverTimeService driverTimeService;
    @Autowired
    private SettingUtils settingUtils;
    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;


    @Override
    @Async
    public void onApplicationEvent(DriverAutoOfflineEvent event) {

        Driver driver = (Driver) event.getSource();

        logger.debug("AutoOffline : {} is offline.", driver.getUsername());

        //查询司机是否有未完成的接送
//		int applyWorkCar = -1;
//		
//		List<WorkCarPickup> wcps=workCarPickupService.listOldWjAndJsAndSc(driver.getId());
//		if(!CollectionUtils.isEmpty(wcps)){
//			if(wcps.get(0).getStatus()==WorkCarPickup.WORKCAR_WEIJIE || wcps.get(0).getStatus()==WorkCarPickup.WORKCAR_YIJIE){
//				applyWorkCar = WorkCarPickup.WORKCAR_WEIJIE;
//			}
//			else if(wcps.get(0).getStatus()==WorkCarPickup.WORKCAR_SHANGCHE){
//				applyWorkCar = WorkCarPickup.WORKCAR_YIJIE;
//			}
//		}

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "driverOffline");
        jsonObject.put("driverID", driver.getId());
        jsonObject.put("driverName", driver.getName());
//		jsonObject.put("applyWorkCar", applyWorkCar);
        Company mainCompany = companyService.getMain();

        Company driverCompany = companyService.findOne(driver.getCompanyId());
        WebSocketSend.boardcastToArea(jsonObject, driverCompany.getCityName(), mainCompany.getId());

        try {
            //发送设备推送
            boolean getuiOnline = false;
            if (StringUtils.isNotBlank(driver.getUserID())) {
                getuiOnline = GeTuiEmployV4.getClientOnline(driver.getUserID(), settingUtils.get());
            }

            if (getuiOnline) {
                GeTuiEmployV4.message(driver, "离线通知", "网络异常，请打开软件点击刷新！", settingUtils.get());
            } else {
                if (StringUtils.isNotBlank(driver.getRegistrationId())) {
                    JiGuangEmployV4.notice(driver, "离线通知", "网络异常，请打开软件点击刷新！", settingUtils.get());
                }
            }

        } catch (IOException e) {
            logger.debug("push error : " + e.getMessage());
        }

        //下班记录
        DriverTimeEvent driverTimeEvent = new DriverTimeEvent("offline", driver);
        applicationEventMulticaster.multicastEvent(driverTimeEvent);


    }

}
